<template>
	<view class="indexPage">
		<swiper circular indicator-dots autoplay :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in lunbo" :key="item.id">
				<image :src="item.img" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<!-- 导航区域 -->
		<view class="nav">
			<view v-for="(item,index) in nav" :key="item.index" class="" @tap="open(index)">
				<text class="iconfont" :class="item.ico"></text>
				<text>{{item.txt}}</text>
			</view>
		</view>

		<!-- 商品详情 -->
		<goods-list :goods="goods" :tit="true" @goGoodsDetail="goGoodsDetail" />
		<!-- 数据加载完后 -->
		<view class="isOver" v-if="flag">---我是有底线的---</view>
	</view>
</template>

<script>
	import Vuex from "vuex";
	import goodsList from "@/components/goods-list.vue";
	export default {
		name: "index",
		components: {
			goodsList
		},
		onShow() {
			// 设置底部的tabBar栏购物车的数量
			this.setTabBar();
		},
		onLoad() {
			// 获取轮播图的数量
			this.getSwipers();
			// 获取商品详情信息
			this.getGoods();
		},
		data() {
			return {
				lunbo: [],
				// 导航区域的内容
				nav: [{
					ico: "icon-shangpin-tianchong",
					txt: "商品超市"
				}, {
					ico: "icon-guanyuwomen",
					txt: "联系我们"
				}, {
					ico: "icon-tupian",
					txt: "社区图片"
				}, {
					ico: "icon-shipin",
					txt: "学习视频"
				}],

				// 商品数量
				goods: [],
				// 设置页面获取的数据量
				limit: 6,
				// 跳过多少条数据量
				skip: 0,
				// 商品列表总数
				total: void 0,
				// 是否显示触底(数据已经加载完了)
				flag: false
			};
		},
		methods: {
			...Vuex.mapMutations(["setTabBar"]),
			async getSwipers() {
				let res = await uniCloud.callFunction({
					name: 'getlunbo',
					data: {
						methods: "get"
					}
				});
				this.lunbo = res.result.data;
			},

			// 页面的跳转
			open(index) {
				let url = void 0;
				switch (index) {
					case 0:
						// 商品列表
						url = "../../supermarket/supermarket";
						break;
					case 1:
						// 联系我们
						url = "../../contact/contact";
						break;
					case 2:
						// 社区图片
						url = "../../pics/pics";
						break;
					default:
						url = "../../videoTutorial/videoTutorial";
						break;
				};
				// 如果index不等于0,就使用以下方法跳转页面(不用传递数据)
				if (index !== 0) {
					uni.navigateTo({
						url
					});
					return;
				};

				uni.navigateTo({
					url,
					success: res => {
						res.eventChannel.emit('indexPage', {
							data: this.goods,
							total: this.total
						})
					}
				});
			},
			// 获取热门商品的数据
			async getGoods() {
				let res = await uniCloud.callFunction({
					name: 'goods',
					data: {
						methods: "get",
						limit: this.limit,
						skip: this.skip,
					}
				});
				// 商品列表
				this.goods = [...this.goods, ...res.result.data.data];
				// 商品总数
				this.total = res.result.total.total;
				this.skip = this.skip + this.limit;
			},

			// 跳转到导航的商品详情页
			goGoodsDetail(item) {
				uni.navigateTo({
					url: "../../goodsDetail/goodsDetail",
					success: res => {
						res.eventChannel.emit('goodsDetail', {
							item
						});
					}
				});
			},
		},
		// 触底时触发该事件
		onReachBottom() {
			if (this.goods.length < this.total) {
				this.getGoods();
			} else {
				this.flag = true;
			};
		},
	}
</script>
<style lang="scss">
	swiper {
		width: 750rpx;

		swiper-item {
			height: 350rpx;
			width: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}


	// 导航栏社区
	.nav {
		@include flexBetweenCenter;
		margin: 20rpx 0 30rpx 0;

		view {
			flex: 1;
			@include flexCenter;
			flex-direction: column;

			.icon-tupian {
				font-size: 42.5rpx !important;
			}

			.icon-guanyuwomen {
				font-size: 57rpx !important;
			}

			>text {
				&:first-of-type {
					@include flexCenterCenter;
					background-color: #b50e03;
					height: 100rpx;
					width: 100rpx;
					border-radius: 50%;
					font-size: 50rpx;
					color: #FFFFFF;
				}

				&:last-of-type {
					margin-top: 10rpx;
					font-size: 30rpx;
					letter-spacing: 2rpx;
				}
			}
		}
	}
</style>
